package com.example.shortlink.controller;

import com.example.shortlink.service.ShortUrlService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;


/**
 * @author lihongmin
 * @since 2023/03/06
 **/
@RestController
@RequestMapping(path = "/")
public class ShortUrlController {

    private static final Logger log = LoggerFactory.getLogger(ShortUrlController.class);

    @Resource
    ShortUrlService service;

    /**
     * 生成短链接
     *
     * @param url 长链接
     * @return 短链接
     */
    @GetMapping(path = "/generator")
    public String generator(@RequestParam String url) {
        return service.generator(url);
    }

    /**
     * 根据短链接重定向到原始链接
     *
     * @param url 短链接
     */
    @GetMapping(path = "/{url}")
    public ModelAndView getOriginalUrl(@PathVariable String url) {
        return service.getOriginalUrl(url);
    }

    /**
     * 测试短链接请求
     */
    @GetMapping(path = "/print")
    public String print(@RequestParam String message) {
        log.info("打印参数：{}", message);
        return message;
    }

}
